﻿// <copyright file="PlayerViewModel.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace Channel9Kinect
{
    #region

    using System;
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Windows.Controls;
    using System.Windows.Input;

    using Channel9Kinect.Input;
    using Channel9Kinect.Services;

    using MediaRss;

    #endregion

    internal class PlayerViewModel : INotifyPropertyChanged
    {
        #region Constants and Fields

        private Uri defaultImage;

        private MediaRssItem item;

        private MediaElement mediaElement;

        private Uri mediaSource;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///   Initializes a new instance of the <see cref = "MainViewModel" /> class.
        /// </summary>
        public PlayerViewModel()
        {
            ServiceLocator.Instance.SatisfyImportsOnce(this);

            this.Play = new Command(() =>
                {
                    this.MediaElement.Play();
                    this.OnStartPlaying();
                });
            this.Back = new Command(() => this.NavigationServiceWrapper.NavigationService.GoBack());
        }

        #endregion

        #region Public Events

        /// <summary>
        ///   The property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        public event EventHandler StartPlaying;

        #endregion

        #region Public Properties

        public ICommand Back { get; private set; }

        public Uri DefaultImage
        {
            get
            {
                return this.defaultImage;
            }
            set
            {
                if (this.defaultImage != value)
                {
                    this.defaultImage = value;
                    this.RaisePropertyChanged(n => n.DefaultImage);
                }
            }
        }

        public MediaRssItem Item
        {
            get
            {
                return this.item;
            }
            set
            {
                if (this.item != value)
                {
                    this.item = value;
                    if (value != null)
                    {
                        this.MediaSource = value.ContentsNode.ContentsItems.Where(_ => _.Url.ToString().EndsWith(".wmv", StringComparison.OrdinalIgnoreCase)).Select(_ => _.Url).LastOrDefault();
                    }
                    this.RaisePropertyChanged(n => n.Item);
                }
            }
        }

        public MediaElement MediaElement
        {
            get
            {
                return this.mediaElement;
            }
            set
            {
                if (this.mediaElement != value)
                {
                    this.mediaElement = value;
                    this.RaisePropertyChanged(n => n.MediaElement);
                }
            }
        }

        public Uri MediaSource
        {
            get
            {
                return this.mediaSource;
            }
            set
            {
                if (this.mediaSource != value)
                {
                    this.mediaSource = value;
                    this.RaisePropertyChanged(n => n.MediaSource);
                }
            }
        }

        [Import]
        public NavigationServiceWrapper NavigationServiceWrapper { get; set; }

        public ICommand Play { get; private set; }

        #endregion

        #region Methods

        protected virtual void OnStartPlaying()
        {
            if (this.StartPlaying != null)
            {
                this.StartPlaying(this, EventArgs.Empty);
            }
        }

        /// <summary>
        ///   The raise property changed.
        /// </summary>
        /// <param name = "expression">
        ///   The expression.
        /// </param>
        /// <typeparam name = "TProperty">
        /// </typeparam>
        protected void RaisePropertyChanged<TProperty>(Expression<Func<PlayerViewModel, TProperty>> expression)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(this.GetPropertyName(expression)));
            }
        }

        #endregion
    }
}